//package cn.xie.blog.servergateway.handler;
//
//import cn.hutool.extra.spring.SpringUtil;
//import cn.xie.blog.servergateway.ro.User;
//import cn.xie.blog.serverutil.util.JwtUtil;
//import cn.xie.blog.model.common.enums.response.ResponseCodeEnum;
//import cn.xie.blog.serverutil.constant.jwt.JwtContants;
//import cn.xie.blog.serverutil.util.RedisUtil;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jakarta.annotation.Resource;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.ServletOutputStream;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//
///**
// * @description: 登录成功处理器
// * @author: xiaoxie
// * @date: 2025/4/30 上午10:58
// * @param:
// * @return:
// **/
//@Component
//public class LoginSuccessHandler implements AuthenticationSuccessHandler {
//
//    private final Logger logger = LoggerFactory.getLogger(LoginSuccessHandler.class);
//    @Resource
//    private  ObjectMapper objectMapper;
//    @Resource
//    private JwtUtil jwtUtil;
//
//    @Override
//    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        logger.info("用户登录成功：{}", authentication.getName());
//        logger.debug("请求URL：{}", request.getRequestURI());
//
//        User user = (User) authentication.getPrincipal();
//        String token = genToken(user.getUsername());
//        response.setContentType("application/json;charset=UTF-8");
//        ServletOutputStream out = response.getOutputStream();
//        out.write(objectMapper.writeValueAsString(ResponseCodeEnum.SUCCESS).getBytes(StandardCharsets.UTF_8));
//        out.flush();
//        out.close();
//    }
//    /**
//     * @description: 生成令牌
//     * @author: xiaoxie
//     * @date: 2025/4/30 上午10:58
//     * @param: [username]
//     * @return: java.lang.String
//     **/
//    public String genToken(String username) {
//        logger.debug("为用户生成令牌：{}", username);
//        String token = jwtUtil.getUserNameFromToken(username);
//        SpringUtil.getBean(RedisUtil.class).put(JwtContants.TOKEN_PREFIX + username, token, JwtContants.getExpireDate().getTime());
//        SpringUtil.getBean(RedisUtil.class).put(JwtContants.REFRESH_TOKEN_PREFIX + username, token, JwtContants.getExpireDate().getTime());
//
//        logger.debug("令牌和刷新令牌已存储到Redis：{}", username);
//        return token;
//    }
//}